/**
 * @Description 登陆view
 * @author lirenyi
 * @update 2016-06-10
 */
define(function(require, exports, modules) {
	'use strict';

	var template = require('text!./tpl.base.html'),
		util = require('util');
	var View = Backbone.View.extend({
		el : '#mybody',
		events : {
			'click .common-alert .cancelBtn':'cancelAlert',
			'click .common-alert .confirmBtn':'confirmAlert',
			'touchstart #fenxiang':'fenxiang',
			'touchmove #fenxiang':'fenxiang',
			'touchend #fenxiang':'fenxiang',
		},
		initialize : function() {
			this.fenxiangStatus = false;
			this.template = _.template(template);
			this.render();
		},
		render : function() {
			this.$el.html(this.template());
			this.getOs();
			var userStr = util.getCookie('user');
			if(userStr){
				var user = $.parseJSON(userStr);
				if(user.musicFlag == '1'){
					$('audio')[0].play();
				}else{
					$('audio')[0].pause();
				}
				var userRefId = user.userRefId;	//邀请码
				$('#userRefId').html(userRefId);
				var match = window.location.href.match(new RegExp("(#)([^(\\?|#)]*)(\\?|$)"));
				var hash = match ? match[2] : ' ';
				console.log('hash',hash);
				if(['home','farm','friendFarm','chickensLog','records'].indexOf(hash)  !== -1){
					$('#fenxiang').show();
				}else{
					$('#fenxiang').hide();
				}
			}else{
				$('audio')[0].play();
			}
			
			!function dog(){
				var eX,eY;
				var dogX,dogY;
				var touchInnerX,touchInnerY;
				var dog = document.querySelector('#fenxiang');
				var dogWidth = dog.offsetWidth;
				var dogHeight= dog.offsetHeight;
				var dogMaxX = $('#page').width()-dogWidth,
					dogMaxY= document.querySelector('#page').offsetHeight-dogHeight;
				dog.style.top = '10px';
				dog.style.left = '10px';

				dog.addEventListener('touchstart',touch, false);
				dog.addEventListener('touchmove',touch, false);
				dog.addEventListener('touchend',touch, false);
				var status = false;

				function touch (event){
					var event = event || window.event;
					
					switch(event.type){
						case "touchstart":
							event.preventDefault();
							eX = event.touches[0].clientX;
							eY = event.touches[0].clientY;
							dogX = dog.offsetLeft;
							dogY = dog.offsetTop;
							touchInnerX = eX-dogX;
							touchInnerY = eY-dogY;
							status = true;
							break;
						case "touchend":
							event.preventDefault();
							if(status){
								//点击分享
								Backbone.history.navigate('share',{trigger:true});
							}
							status = false;
							break;
						case "touchmove":
							event.preventDefault();
							dogX = event.touches[0].clientX - touchInnerX;
							dogY = event.touches[0].clientY - touchInnerY;
							if(dogX>dogMaxX){
								dog.style.left = dogMaxX+'px';
							}else if(dogX<0){
								dog.style.left = 0;
							}else {
								dog.style.left = dogX+'px';
							}
							if(dogY<0){
								dog.style.top = 0;
							}else if(dogY>dogMaxY){
								dog.style.top = dogMaxY+'px';
							}else{
								dog.style.top = dogY+'px';
							}
							status = false;
							break;
					}
				}
			}();
		},
		cancelAlert:function(){
			$('.common-alert').remove();
		},
		confirmAlert:function(){
			if($('.common-alert').hasClass('timeout')){
				$('.common-alert').remove();
				Backbone.history.navigate('login',{trigger:true});
			}
			$('.common-alert').remove();
		},
		getOs:function(){
			var userAgent = navigator.userAgent.toLowerCase();
			var mobile = userAgent.match(/(iphone|ipod|android|ios|ipad)/i);
			var isPC = !mobile;
			if(isPC){
				var winset = function (){
					$('#page').removeClass('page').addClass('pc');
					var ch = document.body.clientHeight;
					var w = ch*9/16;
					$('#page').css({width: w +'px',height: ch + 'px'});
					$('html').css({'font-size': '18px'});
				}
				window.onresize = function () {
					winset();
				}
				$(document).ready(function(){
					winset();
				});
			}
		}
	});

	return View;
});